package com.ljx.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.ljx.JwtUtils;
import com.ljx.MD5;
import com.ljx.R;
import com.ljx.exception.GuliException;
import com.ljx.pojo.UcenterMember;
import com.ljx.mapper.UcenterMemberMapper;
import com.ljx.pojo.vo.RegisterVo;
import com.ljx.service.UcenterMemberService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.apache.velocity.runtime.directive.Parse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;
import org.springframework.web.client.RestTemplate;

import java.util.WeakHashMap;

/**
 * <p>
 * 会员表 服务实现类
 * </p>
 *
 * @author 李加喜
 * @since 2020-09-29
 */
@Service
public class UcenterMemberServiceImpl extends ServiceImpl<UcenterMemberMapper, UcenterMember> implements UcenterMemberService {

    @Autowired
    private RedisTemplate<String,String> redisTemplate;
    //实现用户的登录
    @Override
    public String login(UcenterMember ucenterMember) {

        //QueryWrapper<UcenterMember> wrapper = new QueryWrapper<>();
        //设置传入的用户名与密码进行查询是可以的但是现在不通过这种方式
        //因为这种方式无法实现多个模块共享

        //使用单点登录
        //获取所需要的信息
        String phone=ucenterMember.getMobile();
        String password=ucenterMember.getPassword();
        //手机号或者密码为空的话就无法进行判断
        if (StringUtils.isEmpty(phone) || StringUtils.isEmpty(password)){
            throw new GuliException(20001,"手机号或密码不能为空");
        }
        //查询手机号是否正确
        QueryWrapper<UcenterMember> wrapper = new QueryWrapper<>();
        wrapper.eq("mobile",phone);
        UcenterMember member = baseMapper.selectOne(wrapper);
        if (member==null){
            throw new GuliException(20001,"手机号存不存在");
        }
        //判断密码
        //因为存储到数据库的密码一定是经过加密的
        //所以需要将输入的密码进行MD5加密之后再和数据库的密码进行比较
        //加密方式MD5  自定义的方法进行加密
        if (!MD5.encrypt(password).equals(member.getPassword())){
            throw new GuliException(20001,"对不起密码错误,登录失败");
        }
        //判断用户是否是禁用的状态
        if (member.getIsDisabled()){
            throw new GuliException(20001,"对不起用户处于禁用状态,登录失败");
        }
        //都没有问题则表明登录成功 生成Token  后面可以根据Token获取用户的Id 进而获取用户详细信息
        String token = JwtUtils.getJwtToken(member.getId(), member.getNickname());
        return token;
    }

    @Override
    public void registerUser(RegisterVo registerVo) {
        //获取注册的数据
        String code = registerVo.getCode(); //验证码
        String mobile = registerVo.getMobile(); //手机号
        String nickname = registerVo.getNickname(); //昵称
        String password = registerVo.getPassword(); //密码

        //非空判断
        if(StringUtils.isEmpty(mobile) || StringUtils.isEmpty(password)
                || StringUtils.isEmpty(code) || StringUtils.isEmpty(nickname)) {
            throw new GuliException(20001,"注册失败");
        }
        //判断验证码
        //获取redis验证码
        String redisCode = redisTemplate.opsForValue().get(mobile);

        if(!code.equals(redisCode)) {
            //System.out.println(code.getBytes());
            //System.out.println(code.length()+"--");
            //System.out.println(redisCode.getBytes());
            //System.out.println(redisCode.length()+"+++");
            throw new GuliException(20001,"验证码错误");
        }

        //判断手机号是否重复，表里面存在相同手机号不进行添加
        QueryWrapper<UcenterMember> wrapper = new QueryWrapper<>();
        wrapper.eq("mobile",mobile);
        Integer count = baseMapper.selectCount(wrapper);
        if(count > 0) {
            throw new GuliException(20001,"注册失败");
        }

        //将数据添加到数据库里面
        UcenterMember member = new UcenterMember();
        String md5Pass=MD5.encrypt(password);
        member.setNickname(nickname).setMobile(mobile).setPassword(md5Pass)
                .setIsDisabled(false)
                .setAvatar("http://thirdwx.qlogo.cn/mmopen/vi_32/zZfLXcetf2Rpsibq6HbPUWKgWSJHtha9y1XBeaqluPUs6BYicW1FJaVqj7U3ozHd3iaodGKJOvY2PvqYTuCKwpyfQ/132");
        baseMapper.insert(member);
    }

    @Override
    public UcenterMember getOpenId(String openid) {
        QueryWrapper<UcenterMember> wrapper = new QueryWrapper<>();
        wrapper.eq("openid",openid);
        UcenterMember member = baseMapper.selectOne(wrapper);
        return member;
    }
    //查询在某一天里面注册的用户数量
    @Override
    public Integer countRegister(String day) {
        int count=baseMapper.countRegister(day);
        return count;
    }


}
